package org.mcjug;

import org.mcjug.model.StatusData;
import org.mcjug.model.UpdateIntentService;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class TimelineActivity extends BaseActivity {
	private static String TAG = TimelineActivity.class.getSimpleName();
	private StatusData statusData;
	private SimpleCursorAdapter adapter;

	static final String[] FROM = { StatusData.C_CREATED_AT, StatusData.C_USER,
			StatusData.C_TEXT };
	static final int[] TO = { R.id.rowDateTv, R.id.rowUserTv, R.id.rowTextTv };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.statusData = new StatusData(getApplicationContext());
		setContentView(R.layout.timeline);
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(
				timelineReceiver,
				new IntentFilter(UpdateIntentService.BROADCAST_INTENT_NAME),
				getString(R.string.send_timeline_notifications_permission_name),
				null);
		refreshListView();
	}

	void refreshListView() {
		cursor = statusData.getStatusUpdates();
		startManagingCursor(cursor);
		adapter = new SimpleCursorAdapter(getApplicationContext(),
				R.layout.row, cursor, FROM, TO);
		adapter.setViewBinder(VIEW_BINDER);
		((ListView) findViewById(R.id.timeline_listview_id))
				.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		statusData.close();
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(timelineReceiver);
	}

	static final ViewBinder VIEW_BINDER = new ViewBinder() {

		public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
			if (view.getId() != R.id.rowDateTv) {
				return false;
			}
			long timeStamp = cursor.getLong(cursor
					.getColumnIndex(StatusData.C_CREATED_AT));
			CharSequence reltime = DateUtils
					.getRelativeTimeSpanString(timeStamp);
			((TextView) view).setText(reltime);
			return true;
		}
	};

	BroadcastReceiver timelineReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			Log.d(TAG, "got intent: " + arg1.getAction());
			if (arg1.getAction().equals(
					UpdateIntentService.BROADCAST_INTENT_NAME)) {
				Log.d(TAG, "intent passed the if check ");
				stopManagingCursor(cursor);
				cursor.close();
				cursor = statusData.getStatusUpdates();
				startManagingCursor(cursor);
				adapter = new SimpleCursorAdapter(getApplicationContext(),
						R.layout.row, cursor, FROM, TO);
				adapter.setViewBinder(VIEW_BINDER);
				((ListView) findViewById(R.id.timeline_listview_id))
						.setAdapter(adapter);
				Log.d(TAG, "done with work");
			}
		}
	};
	private Cursor cursor;
}
